package spacecrack;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;

public class PlayerManager {
	@SuppressWarnings("unchecked")
	public void clearPlayers() {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			final List<Player> players = (List<Player>) query.execute();

			pm.deletePersistentAll(players);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public void clearPlayer(String id) {
		if (id == null) throw new IllegalArgumentException();
		if (id.isEmpty()) throw new IllegalArgumentException();

		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Player> players = (List<Player>) query.execute(Player.getKey(id));
			for (Player player : players) {
				pm.deletePersistent(player);
			}
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public boolean playerExists(String id) {
		if (id == null) throw new IllegalArgumentException();
		if (id.isEmpty()) throw new IllegalArgumentException();

		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Player> players = (List<Player>) query.execute(Player.getKey(id));
			return (!players.isEmpty());
		} finally {
			pm.close();
		}
	}

	public void storePlayer(Player player) {
		if (player == null) throw new IllegalArgumentException();

		final PersistenceManager pm = PMF.getPM();

		try {
			pm.makePersistent(player);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Player loadPlayerById(String id) {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Player> players = (List<Player>) query.execute(Player.getKey(id));

			if (players.isEmpty()) {
				throw new IllegalArgumentException("PlayerID is unknown");
			}
			if (players.size() > 1) {
				throw new IllegalArgumentException("PlayerID is ambigious");
			}

			return players.get(0);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Player loadPlayerByName(String name) {
		if (name == null) throw new IllegalArgumentException();
		if (name.isEmpty()) throw new IllegalArgumentException();

		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			query.setFilter("name == nameParam");
			query.declareParameters("String nameParam");

			final List<Player> players = (List<Player>) query.execute(name);

			if (players.isEmpty()) {
				throw new IllegalArgumentException("PlayerID is unknown");
			}
			if (players.size() > 1) {
				throw new IllegalArgumentException("PlayerID is ambigious");
			}

			return players.get(0);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Player> loadPlayers() {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Player.class);
			final List<Player> players = (List<Player>) query.execute();

			return new ArrayList<Player>(players);
		} finally {
			pm.close();
		}
	}
}
